<!DOCTYPE HTML>
<html lang="en">

<head>
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="lixiaodaoaaa 的 个人博客">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <meta name="theme-version" content="1.2.3">
    <meta name="root" content="/blog/">
    <link rel="dns-prefetch" href="https://gitee.com/lixiaodaoaaa/blog">
    <!--SEO-->

<meta name="keywords" content="" />


<meta name="description" content="之前的做法 经常要请求网络 进行服务端联调，Server返回有部分json，我们要快速知道对方json里特定字符串数据。通用做法就是复制出来结果，然后到特定网站上格式化一下，然后拿出来看一下，找..." />


<meta name="robots" content="all" />
<meta name="google" content="all" />
<meta name="googlebot" content="all" />
<meta name="verify" content="all" />
    <!--Title-->

<title>
    
    通过Alfred一键格式化剪贴板中的JSON |
    
    lixiaodaoaaa 的 个人博客
</title>

<link rel="alternate" href="/atom.xml" title="lixiaodaoaaa 的 个人博客" type="application/atom+xml">


<link rel="icon" href="/favicon.ico">

    

<link rel="stylesheet" href="/blog/css/bootstrap.min.css?rev=3.3.7">
<link rel="stylesheet" href="/blog/css/font-awesome.min.css?rev=4.7.0">
<link rel="stylesheet" href="/blog/css/style.css?rev=@@hash">
    
<div class="hide">
    <script type="text/javascript">
    var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
    document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
    </script>
</div>




    

</head>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->
<body>
    <header class="main-header"  style="background-image:url(
    http://snippet.shenliyang.com/img/banner.jpg)"
     >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title='lixiaodaoaaa'>
            <img src="/blog/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
            <!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
            <img src="/blog/img/branding.png" alt="Snippet 博客主题" class="img-responsive center-block">
            
        </div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                        <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="https://gitee.com/lixiaodaoaaa/blog">
                        lixiaodaoaaa 的 个人博客</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog"><i class="fa "></i>
                                主页</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/哲科思辨/"><i class="fa "></i>
                                哲科思辨</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/Android开发/"><i class="fa "></i>
                                Android开发</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/archives/"><i class="fa "></i>
                                时间轴</a>
                        </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="通过Alfred一键格式化剪贴板中的JSON">
            
            通过Alfred一键格式化剪贴板中的JSON
            
        </h1>
        <div class="post-meta">
    
    <span class="categories-meta fa-wrap">
        <i class="fa fa-folder-open-o"></i>
        
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
        </span>
    </span>
    
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta">
            2022/02/17</span>
    </span>
    
    
</div>
        
        
    </div>
    
    <div class="post-body post-content">
        <h3 id="之前的做法"><a href="#之前的做法" class="headerlink" title="之前的做法"></a>之前的做法</h3><p> 经常要请求网络 进行服务端联调，Server返回有部分json，我们要快速知道对方json里特定字符串数据。通用做法就是复制出来结果，然后到特定网站上格式化一下，然后拿出来看一下，找到特定位置。。等等。</p>
<h3 id="存在的问题"><a href="#存在的问题" class="headerlink" title="存在的问题"></a>存在的问题</h3><ul>
<li>剪贴板中有包含其他除了JSon的部分（需要通过脚本过滤出JSon字符串）</li>
<li>找到json部分 需要格式化一下（美观、好查、方便 快捷）</li>
</ul>
<h3 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h3><p>百度了下找到了有人类似的解决方法如下:<br><img src="http://upload-images.jianshu.io/upload_images/658671-241fbf70f0cc002a.gif?imageMogr2/auto-orient/strip" alt="image"></p>
<h3 id="思路分两步。"><a href="#思路分两步。" class="headerlink" title="思路分两步。"></a>思路分两步。</h3><ul>
<li><p>第一步拿到剪贴板的数据，抽到JSon字符串 再扔进剪贴板<br>具体抽取方法 从整个剪贴板中 找到第一个 <em>“{“</em>  再找到最后一个 <em>“}”</em> </p>
</li>
<li><p>再对剪贴板的数据进行快速格式化</p>
<h3 id="alfred操作步骤"><a href="#alfred操作步骤" class="headerlink" title="alfred操作步骤"></a>alfred操作步骤</h3><p><img src="http://upload-images.jianshu.io/upload_images/658671-647ab1bfaa0329ef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br> 至于alfred是啥 不做多介绍。可以定义快捷键直接一键操作 不用大动干戈，执行脚本等。</p>
</li>
</ul>
<h4 id="第一步脚本如下："><a href="#第一步脚本如下：" class="headerlink" title="第一步脚本如下："></a>第一步脚本如下：</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> subprocess</span><br><span class="line"></span><br><span class="line">p = subprocess.Popen([<span class="string">'pbpaste'</span>], stdout=subprocess.PIPE)</span><br><span class="line">retcode = p.wait()</span><br><span class="line">data = p.stdout.read()</span><br><span class="line">firstBrace = data.index(<span class="string">"&#123;"</span>)</span><br><span class="line">lastBrace = data.rindex(<span class="string">"&#125;"</span>) + <span class="number">1</span></span><br><span class="line">strNeedFormatter = data[firstBrace:lastBrace]</span><br><span class="line">p = subprocess.Popen([<span class="string">'pbcopy'</span>], stdin=subprocess.PIPE)</span><br><span class="line">p.stdin.write(strNeedFormatter)</span><br><span class="line">p.stdin.close()</span><br><span class="line">p.communicate()</span><br></pre></td></tr></table></figure>
<h4 id="第二步脚本如下：（直接将剪贴板的数据格式化-再扔进剪贴板）"><a href="#第二步脚本如下：（直接将剪贴板的数据格式化-再扔进剪贴板）" class="headerlink" title="第二步脚本如下：（直接将剪贴板的数据格式化 再扔进剪贴板）"></a>第二步脚本如下：（直接将剪贴板的数据格式化 再扔进剪贴板）</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">!/usr/bin/env bash</span></span><br><span class="line">pbpaste | python -m json.tool  &gt; temp.txt</span><br><span class="line">cat temp.txt | pbcopy</span><br></pre></td></tr></table></figure>

<h3 id="最后得到的效果"><a href="#最后得到的效果" class="headerlink" title="最后得到的效果"></a>最后得到的效果</h3><p> 从剪贴板复制任意包含json的，按一下快捷键 剪贴板的内容神奇的发生了变化（已经去除无用内容，并且已经格式化） 目前暂定快捷键是 <strong>ctrol+option+comoand+j</strong>.</p>
<h3 id="alfred下载地址"><a href="#alfred下载地址" class="headerlink" title="alfred下载地址"></a>alfred下载地址</h3><p>祝你好运！！一键格式化<br>下载地址失效。<br>最新下载地址：<br><a href="https://share-tools.gz.bcebos.com/FormattherClipboardJson.zip?authorization=bce-auth-v1/5619cfe06a994533b043e74ecf2cb1a9/2019-11-12T02:04:50Z/-1/host/4ef5fb34a49bb8b2b3824b76b722898a2f46c4f9ef883da366f13b8c37c77d8e" target="_blank" rel="noopener">猛击这里超链接下载</a></p>
<h3 id="有两个格式化剪贴板JSon的工作流。"><a href="#有两个格式化剪贴板JSon的工作流。" class="headerlink" title="有两个格式化剪贴板JSon的工作流。"></a>有两个格式化剪贴板JSon的工作流。</h3><ul>
<li>第一种触发 ：激活alfred 输入关键字“j” 回车自动格式化</li>
<li>快捷键触发：<strong>commnd+option+ctrol+J</strong></li>
<li>快捷键可以自己修改。请自行修改文件即可。</li>
<li>下载文件导入到Alfred中。即可使用</li>
</ul>

    </div>
    
    <div class="post-footer">
        <div>
            
            转载声明：
            商业转载请联系作者获得授权,非商业转载请注明出处 © <a href="" target="_blank">Snippet</a>
            
            
        </div>
        <div>
            
        </div>
    </div>
</article>
<div class="article-nav prev-next-wrap clearfix">
    
    
    <a href="/blog/2022/02/17/%E4%B8%96%E7%95%8C%E4%B8%8A%E6%9C%89%E4%B8%A4%E7%A7%8D%E4%BA%BA%E7%9A%84%E8%AF%9D%E6%98%AF%E4%B8%8D%E8%83%BD%E4%BF%A1%E7%9A%84/" class="next-post btn btn-default" title='世界上有两种人的话是不能信的'>
        <span class="hidden-lg">下一篇</span>
        <span class="hidden-xs">
            世界上有两种人的话是不能信的</span><i class="fa fa-angle-right fa-fw"></i>
    </a>
    
</div>

<div id="comments">
    

<div id="vcomments" class="valine"></div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="/blog/assets/valine.min.js"></script>
<script>
new Valine({
    av: AV,
    el: '#vcomments',
    appId: 'xOKV9J4UeQAtVkvnJC7Kq2Jn-gzGzoHsz',
    appKey: 'erIpQac4azoCmgfBB7Dl9maa',
    placeholder: '说点什么吧',
    notify: false,
    verify: true,
    avatar: 'mm',
    meta: 'nick,mail'.split(','),
    pageSize: '10',
    path: window.location.pathname,
    lang: 'en'.toLowerCase()
})
</script>


</div>

                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">
            Table of Contents
        </h3>
        
        <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#之前的做法"><span class="toc-text">之前的做法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#存在的问题"><span class="toc-text">存在的问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解决方案"><span class="toc-text">解决方案</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#思路分两步。"><span class="toc-text">思路分两步。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#alfred操作步骤"><span class="toc-text">alfred操作步骤</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#第一步脚本如下："><span class="toc-text">第一步脚本如下：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#第二步脚本如下：（直接将剪贴板的数据格式化-再扔进剪贴板）"><span class="toc-text">第二步脚本如下：（直接将剪贴板的数据格式化 再扔进剪贴板）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#最后得到的效果"><span class="toc-text">最后得到的效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#alfred下载地址"><span class="toc-text">alfred下载地址</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#有两个格式化剪贴板JSon的工作流。"><span class="toc-text">有两个格式化剪贴板JSon的工作流。</span></a></li></ol>
        
    </div>
</aside>
                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>
<a id="back-to-top" class="icon-btn hide">
    <i class="fa fa-chevron-up"></i>
</a>
    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
</div>
            </div>
            <div class="col-sm-12">
                <span>Copyright &copy;
                    2017
                    
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>



<script src="/blog/js/app.js?rev=@@hash"></script>
</body>
</html>